package com.zlx.sharelive.activity;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.zlx.module_base.base_ac.BaseAc;
import com.zlx.module_base.base_api.res_data.AgreementBean;
import com.zlx.module_base.base_api.util.ApiUtil;
import com.zlx.module_base.base_util.SPUtil;
import com.zlx.module_base.base_util.StringUt;
import com.zlx.module_network.api1.livedata.BaseObserver;
import com.zlx.module_network.api1.livedata.BaseObserverCallBack;
import com.zlx.module_network.bean.ApiResponse;
import com.zlx.sharelive.Constants;
import com.zlx.sharelive.MyApp;
import com.zlx.sharelive.R;

import java.util.Random;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 设置
 */
public class SettingAc extends BaseAc {

    @BindView(R.id.tv_my_phone)
    TextView tv_my_phone;
    @BindView(R.id.tv_code)
    TextView tv_code;
    @BindView(R.id.tv_cache)
    TextView tv_cache;

    @Override
    protected int getLayoutId() {
        return R.layout.ac_setting;
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    showSuccess();
                    Toast.makeText(SettingAc.this, "清除缓存完成", Toast.LENGTH_SHORT).show();
                    tv_cache.setText("0M");
                    break;
                case 2:
                    showSuccess();
                    Toast.makeText(SettingAc.this, "已经是最新版本，无需更新", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    @Override
    public void initViews() {
        super.initViews();

        tv_my_phone.setText((String) SPUtil.get(this, Constants.SP_SAVE_PHONE, ""));
        tv_code.setText(StringgetAppVersionName(this) + "版本");

        String s = "";
        Random ran = new Random(System.currentTimeMillis());
        for (int i = 0; i < 1; i++) {
            s = s + ran.nextInt(100);
        }
        tv_cache.setText(s + "M");
    }


    @OnClick({R.id.iv_back, R.id.bt_back_login, R.id.ll_pass, R.id.ll_pay_pass, R.id.ll_phone,
            R.id.ll_protocol, R.id.ll_code, R.id.ll_cache})
    public void setOnClick(View view) {
        switch (view.getId()) {
            case R.id.ll_cache:
                showLoading();
                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        try {
                            sleep(2000);
                            Message msg = new Message();
                            msg.what = 1;
                            handler.sendMessage(msg);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
                break;
            case R.id.ll_code:
                showLoading();
                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        try {
                            sleep(2000);
                            Message msg = new Message();
                            msg.what = 2;
                            handler.sendMessage(msg);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
                break;
            case R.id.ll_protocol:
                getAgreement("2");
                break;
            case R.id.iv_back:
                finish();
                break;
            case R.id.ll_phone:
                startActivity(new Intent(this, ModifyPhoneAc.class));
                break;
            case R.id.bt_back_login:
                MyApp.getInstance().backToLogin();
                break;
            case R.id.ll_pass:
                startActivity(new Intent(this, PassAc.class));
                break;
            case R.id.ll_pay_pass:
                startActivity(new Intent(this, PayAc.class));
                break;
        }
    }

    private void getAgreement(String type) {
        ApiUtil.getHomeApi().getAgreement(type).observe(this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<AgreementBean>>() {
            @Override
            public void onSuccess(ApiResponse<AgreementBean> data) {
                if (null != data) {
                    if (null != data.getData()) {
                        if (null != data.getData().getInfo()) {
                            if (data.getData().getInfo().size() > 0) {
                                startActivity(new Intent(SettingAc.this, WebViewActivity.class)
                                        .putExtra("type", "2")
                                        .putExtra("title", data.getData().getInfo().get(0).getPost_title())
                                        .putExtra("content", data.getData().getInfo().get(0).getPost_content())
                                );
                            }
                        }
                    }
                }
            }

            @Override
            public void onFinish() {
                super.onFinish();

            }
        }));
    }

    /**
     * 获取版本号
     *
     * @return 版本号
     */

    public String StringgetAppVersionName(Context context) {
        String versionName = "";
        try {
            PackageManager pm = context.getPackageManager();
            PackageInfo p1 = pm.getPackageInfo(context.getPackageName(), 0);
            versionName = p1.versionName;
            if (StringUt.isEmpty(versionName) || versionName.length() <= 0) {
                return "";
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionName;
    }

}
